#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :huaban_获取相似图片.py
# @Time      :2024/10/10 
# @Author    :CL
# @email     :1037654919@qq.com
# desc ：花瓣获取相似图片，https://huaban.com/pins/6316309166/similar/materials
import os
import time
import requests
from utils import mongo_manager
huaban_pins = mongo_manager("huaban_pins", db='car_images')


def get_pin_by_word(text='老年人男', page=1):
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "guest-token": "8ae62ae71cbb4454b3fc04fa46abd6be",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://huaban.com/search?type=pin&q=%E8%80%81%E5%B9%B4%E4%BA%BA%E7%94%B7",
        "sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
        "x-abtest-tags": "eyJ0cmFmZmljX21hdGVyaWFsX2xhYiI6InN0cmluZ18xIiwiaW50ZXJ2ZW50aW9uX3Bpbl9sYWIiOiJzdHJpbmdfMCIsImludGVydmVudGlvbl9tYXRlcmlhbF9sYWIiOiJzdHJpbmdfMCIsInRyYWZmaWNfc2VyYWNoX3Bpbl9sYWIiOiJzdHJpbmdfMCIsIm1hdGVyaWFsX211bHRpcGxlX3RhcmdldCI6InN0cmluZ18xIiwibV9tdWx0aXBsZV90YXJnZXRfZXhwYW5zaW9uIjoic3RyaW5nXzAiLCJ3ZWJfc2VhcmNoX2N0cl92NiI6InN0cmluZ18xIn0=",
        "x-guest-token": "8ae62ae71cbb4454b3fc04fa46abd6be"
    }
    cookies = {
        "user_device_id": "8ae62ae71cbb4454b3fc04fa46abd6be",
        "user_device_id_timestamp": "1728527282358",
        "fd_id": "d34b49db0eeac7da5e22862585a4528e",
        "fd_id_timestamp": "1728527287709",
        "sid": "s%3AU07XHx9ARrLQm7UqFIxptb6d4qF1_10W.xd%2FbX4JAbteWcD0K55ekSMas6YVgGw7540LDaql9PTI",
        "uid": "37256444",
        "gd_id": "2024999432971232265",
        "canary_uuid": "65131fa83c986d9d301fe42d00da9c20",
        "acw_tc": "0b63bb3617301097333117557e5b52ab9194e14c43fdf7671b979b6c4c7d73",
        "huaban-page-setting": "{%22columnType%22:%22full%22%2C%22limit%22:6}",
        "Hm_lvt_d4a0e7c3cd16eb58a65472f40e7ee543": "1728527284,1728724417,1728975819,1730109734",
        "HMACCOUNT": "8A7B5B515FA69801",
        "Hm_lpvt_d4a0e7c3cd16eb58a65472f40e7ee543": "1730110010"
    }
    url = "https://huaban.com/v3/search/file"
    params = {
        "text": text,
        "sort": "all",
        "limit": "40",
        "page": page,
        "position": "search_pin",
        "fields": "pins:PIN|total,facets,split_words,relations,rec_topic_material,topics"
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.url, response)
    if response.status_code == 200:
        return response.json()


def down_image(file, url):  # 下载图片方法
    print("开始下载：", url)
    for i in range(5):
        try:
            response = requests.get(url,  timeout=10)
            with open(file, 'wb') as fd:
                fd.write(response.content)
            requests.session().close()
            return 1
        except Exception as e:
            time.sleep(5)
    print("下载失败了", url)
    if os.path.exists(file):
        os.remove(file)
    return 0

def test():
    page = 1
    while True:
        data = get_similar_materials()
        # print(data)
        pins = data['pins']
        for pin in pins:
            if not huaban_pins.findOne({"_id":pin['pin_id']}):
                datas = {'tags': pin['tags']}
                datas['_id'] = pin['pin_id']
                datas['pin_id'] = pin['pin_id']
                datas['board_id'] = pin['board_id']
                datas['raw_text'] = pin['raw_text']
                datas['file'] = pin['file']
                href = 'https://huaban.com/pins/' + str(pin['pin_id'])
                datas['href'] = href
                src = 'https://gd-hbimg.huaban.com/' + str(pin['file']['key'])
                datas['src'] = src
                    # continue
                name = src.split('/')[-1]
                datas['name'] = name
                # 保存图片
                path = f'/media/chenglei3/77D014CED257D1411/images/huaban/'
                os.makedirs(path, exist_ok=True)
                result = down_image(file=f'{path}{name}', url=src)
                datas['down_status'] = result

                try:
                    huaban_pins.insertOne(datas)
                except Exception as e:
                    huaban_pins.updateOne({"_id":datas["_id"]},datas)
        if len(pins)< 20:
            break
        page +=1

def linshi():
    keywords =['老年人男']
    keywords = [
        # "中国婴儿", "车内中国婴儿",
        # "中国儿童男", "车内中国儿童男",
        # "中国儿童女", "车内中国儿童女",
        # "中国成年人男", "车内中国成年人男",
        # "中国成年人女", "车内中国成年人女",
        "中国老年人男", "车内中国老年人男",
        "中国老年人女", "车内中国老年人女",'老年人女'
    ]
    keywords = [
        "车后排",
        "车内自拍视角",
        "车前排视角看后排",
        "车内前排视角",
        "车内视角",
        '行车记录仪视角',
        '行车记录仪视角车内'
    ]
    for text in keywords:
        page = 1
        while True:
            data = get_pin_by_word(text = text, page=page)

            pins = data['pins']
            for pin in pins:
                if not huaban_pins.findOne({"_id": pin['pin_id']}):
                    datas = {'tags': pin['tags']}
                    datas['_id'] = pin['pin_id']
                    datas['pin_id'] = pin['pin_id']
                    datas['board_id'] = pin['board_id']
                    datas['raw_text'] = pin['raw_text']
                    datas['file'] = pin['file']
                    href = 'https://huaban.com/pins/' + str(pin['pin_id'])
                    datas['href'] = href
                    src = 'https://gd-hbimg.huaban.com/' + str(pin['file']['key'])
                    datas['src'] = src
                    # continue
                    name = src.split('/')[-1]
                    datas['name'] = name
                    # 保存图片
                    path = f'/media/chenglei3/77D014CED257D1411/images/huaban/{text}/'
                    os.makedirs(path, exist_ok=True)
                    result = down_image(file=f'{path}{name}', url=src)
                    datas['down_status'] = result

                    try:
                        huaban_pins.insertOne(datas)
                    except Exception as e:
                        huaban_pins.updateOne({"_id": datas["_id"]}, datas)
            if len(pins) < 20 or page>30: #lisnhi修改
                break
            page += 1


if  __name__ == '__main__':
    print()
    linshi()